import { logout, getInfo } from '@/api/admin'
import { login } from '@/api/auth'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}

const defaultAvatar = 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
const actions = {
  // user login
  login({ commit }, userInfo) {
    const { account, pwd } = userInfo
    console.log('info', userInfo)
    return new Promise((resolve, reject) => {
      console.log('logining')
      login({ account: account.trim(), pwd: pwd }).then(response => {
        const { data } = response
        // console.log(data)

        var realName = data.admin.RealName
        // avatar = data.admin.avatar || ""
        if (realName.length > 0) {
          commit('SET_NAME', realName)
          commit('SET_AVATAR', defaultAvatar)
          commit('SET_TOKEN', data.token)
          setToken(data.token)
          resolve()
        } else {
          reject('用户名为空')
        }
      }).catch(error => {
        // console.log(error)
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { data } = response

        if (!data) {
          reject('Verification failed, please Login again.')
        }

        const { RealName, Roles } = data
        // console.log(data, RealName)

        commit('SET_NAME', RealName)
        commit('SET_AVATAR', defaultAvatar)
        commit('SET_ROLES', Roles.split(','))
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // setInfo({commit, state}) {
  //   return new Promise((resolve, reject) => {
  //     name = state.nickname
  //     avatar = state.avatar
  //     if (name.length>0 ){
  //       commit('SET_NAME', name)
  //       commit('SET_AVATAR', avatar)
  //       resolve("ok")
  //     } else{
  //       reject("用户名为空")
  //     }
  //   })
  // },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // logout(state.token).then(() => {
      removeToken() // must remove  token  first
      resetRouter()
      commit('RESET_STATE')
      resolve()
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

